package session09;

import ATM.FileOperation;
import ATM.UserInfo;
import session09.compent.FontPanel;
import session09.compent.JButtonPanel;
import session09.compent.PanePanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

public class LoginPanel extends PanePanel {

    public static final int MAX_CASH = 200000;
    private int cash = 100000;
    private HashMap<String, UserInfo> allUsers; //把allUsers的类型修改为Map
    private UserInfo theUser;//当前登陆对象

    private JButtonPanel clearBtn;
    private JButtonPanel exitBtn;
    private JButtonPanel sureBtn;

    private FontPanel name;
    private FontPanel pwd;
    private JTextField txt1;
    private JPasswordField txt2;

    public HashMap<String, UserInfo> getAllUsers() {
        return allUsers;
    }

    public void setAllUsers(HashMap<String, UserInfo> allUsers) {
        this.allUsers = allUsers;
    }

    public int getCash() {
        return cash;
    }

    public void setCash(int cash) {
        this.cash = cash;
    }

    public JTextField getTxt1() {
        return txt1;
    }

    public void setTxt1(JTextField txt1) {
        this.txt1 = txt1;
    }

    public JPasswordField getTxt2() {
        return txt2;
    }

    public void setTxt2(JPasswordField txt2) {
        this.txt2 = txt2;
    }

    public LoginPanel(ATMFrame frame) {
        super(200, 225, 200);

        this.allUsers = FileOperation.readFile();

        this.clearBtn = new JButtonPanel("image/buttonClear.jpg", 50, 550);
        this.clearBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                LoginPanel.this.txt1.setText("");
                LoginPanel.this.txt2.setText("");
            }
        });
        this.exitBtn = new JButtonPanel("image/buttonOff.jpg", 442, 600);
        this.exitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Container atm = frame.getContentP();
                CardLayout cardLayout = (CardLayout) atm.getLayout();
                cardLayout.show(atm, "index");
            }
        });
        this.sureBtn = new JButtonPanel("image/buttonSure.jpg", 832, 550);
        this.sureBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Container atm = frame.getContentP();
                CardLayout cardLayout = (CardLayout) atm.getLayout();

                UserInfo user = login();
                if (user != null) {
                    cardLayout.show(atm, "select");
                    frame.getQueryPanel().getTxt1().setText(user.getAccount() + "");
                }else {
                    JOptionPane.showMessageDialog(frame,"账户或密码输入错误");
                }
            }
        });

        this.getBackLab().add(this.clearBtn);
        this.getBackLab().add(this.exitBtn);
        this.getBackLab().add(this.sureBtn);

        this.name = new FontPanel(40, 30, 20, 150, 30);
        this.name.setText("请输入用户名");
        this.pwd = new FontPanel(40, 80, 20, 150, 30);
        this.pwd.setText("请输入密码");
        this.txt1 = new JTextField();
        this.txt2 = new JPasswordField();

        this.getImgLab().add(this.name);
        this.txt1.setBounds(200, 30, 160, 30);
        this.txt1.setFont(new Font("宋体", Font.PLAIN, 18));//输入框中的字体大小
        this.getImgLab().add(this.txt1);
        this.getImgLab().add(this.pwd);
        this.txt2.setBounds(200, 80, 160, 30);
        this.getImgLab().add(this.txt2);
    }

    public UserInfo login() {
        String inputname = this.txt1.getText();
        String inputPwd = new String(this.txt2.getPassword());
        if (this.allUsers.containsKey(inputname)) {
            UserInfo userInfo = this.allUsers.get(inputname);
            if (userInfo.getPassword().equals(inputPwd)) {
                this.theUser = userInfo;
                return this.theUser;
            }
        }
        return null;
    }
}
